#pragma once
#include "..\common\string.h"
#include "..\common\def.h"
#include "memorymanager.h"
#include <stdio.h>

class EE_EXPORT MemoryManager
{
public:

	struct Block { 
		size_t Size; 
		const char* File; 
		unsigned int Line; 
		bool IsArray; 
		Block* Prev; 
		Block* Next; 
	}; 

	static Block* pHead; 
	static Block* pPrev; 
	static bool gStatus; 

private:

	static void InsertBlock(Block *pBlock);
	static void RemoveBlock(Block *pBlock);
	
	static size_t mAllocationCount; 
	static size_t mDeallocationCount; 

	static size_t mAllocatedBytes; 
	static size_t mDeallocatedBytes; 


public:

	static void GetMemoryReport(string &report);
	static void* Allocate(size_t size, bool isArray, const char* file, unsigned int line);
	static void Deallocate(char *pAddr, bool isArray);
}; 
void* operator new (size_t size)
{
	//return malloc(size); 
	return MemoryManager::Allocate(size, false, NULL, 0); 
}

void* operator new[] (size_t size)
{
	//return malloc(size); 
	return MemoryManager::Allocate(size,true, NULL, 0); 
}

void* operator new (size_t size, const char* file, unsigned int line)
{
	//return malloc(size); 
	return MemoryManager::Allocate(size, false, file, line); 
}

void* operator new[] (size_t size, const char* file, unsigned int line)
{
	//return malloc(size); 
	return MemoryManager::Allocate(size, true, file, line); 
}

void operator delete (void *pAddr)
{
	//free(pAddr);  return; 
	MemoryManager::Deallocate((char*)pAddr, false); 
}

void operator delete[] (void* pAddr)
{
	//free(pAddr);  return; 
	MemoryManager::Deallocate((char*)pAddr, true); 
}

